package cn.edu.scau.cmi.linyiqu.singleton;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

public class Controller implements Initializable {
	private static boolean singletonModeFlag = true;

	@FXML
	private Pane pane;
	@FXML
	private Button produceButton;
	@FXML
	private TextArea output;
	@FXML
	private TextField nameInput;
	@FXML
	private Text mode;


	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
	}

	public void switchMode(MouseEvent event){
		output.clear();
		if(singletonModeFlag){
			singletonModeFlag=false;
			mode.setText("多例模式");
		}else{
			singletonModeFlag=true;
			mode.setText("单例模式");
		}
	}


	public void produce(MouseEvent event) {
		String name;
		if(singletonModeFlag && !nameInput.getText().isEmpty()){
			name = nameInput.getText();
			Chairman chairman = Chairman.getInstance(name);
			output.appendText(chairman.getName()+"  "+chairman.toString()+"\n");
		}else if(!singletonModeFlag&&!nameInput.getText().isEmpty()){
			name = nameInput.getText();
			Marshal.getInstance(name);
			output.setText(Marshal.output());
		}
	}

}
